package com.weiyi.mashangzhu.Roomer.Activity;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Fragment.Fragment_Main;
import com.weiyi.mashangzhu.Roomer.Fragment.Fragment_Message;
import com.weiyi.mashangzhu.Roomer.Fragment.Fragment_Order;
import com.weiyi.mashangzhu.Roomer.Fragment.Fragment_Person;
import com.weiyi.mashangzhu.Service.MyIntentService;
import com.weiyi.mashangzhu.Service.MyPushService;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity_R extends BaseActivity {
    @BindView(R.id.rd_group)
    RadioGroup rdGroup;
    @BindView(R.id.rd_menu_main)
    RadioButton rdMenuMain;
    @BindView(R.id.rd_menu_message)
    RadioButton rdMenuMessage;
    @BindView(R.id.rd_menu_order)
    RadioButton rdMenuOrder;
    @BindView(R.id.rd_menu_person)
    RadioButton rdMenuPerson;
    private Fragment_Main fragment_main = new Fragment_Main();
    private Fragment_Message fragment_message = new Fragment_Message();
    private Fragment_Order fragment_order = new Fragment_Order();
    private Fragment_Person fragment_person = new Fragment_Person();
    private FragmentManager fragmentManager;
    private long mExitTime = 0;
    int info;
    public static String ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SPUtils.put(MainActivity_R.this, "loginLeve", "3");
        ButterKnife.bind(this);
        GuideActivity guideActivity = GuideActivity.guideActivity;
        if (guideActivity != null) {
            guideActivity.finish();
        }
        ll = getIntent().getStringExtra("ll");
        LoadingDialog.dismiss(getApplicationContext());
        // com.getui.demo.DemoPushService 为第三方自定义推送服务
        PushManager.getInstance().initialize(this.getApplicationContext(), MyPushService.class);
        // com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), MyIntentService.class);

        info = getIntent().getIntExtra("info", 0);
        initView();
    }

    //    @Override
    public void initView() {
        rdMenuMain.setChecked(true);
        //默认显示的fragment
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (info == 0) {
            transaction.replace(R.id.fragment_container, fragment_main);
            rdGroup.clearCheck();
            rdGroup.check(R.id.rd_menu_main);
        } else {
            transaction.replace(R.id.fragment_container, fragment_order);
            rdGroup.clearCheck();
            rdGroup.check(R.id.rd_menu_order);
        }
        transaction.commit();
        rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                switch (checkedId) {

                    case R.id.rd_menu_main:
                        transaction.replace(R.id.fragment_container, fragment_main);
                        break;
                    case R.id.rd_menu_message:
                        if (SPUtils.get(MainActivity_R.this, "isLogin", "0").equals("0")) {
                            UiUtil.startActivity(MainActivity_R.this, LoginActivity.class);
                        } else if (SPUtils.get(MainActivity_R.this, "isLogin", "0").equals("1")) {
 // 已登录
                            transaction.replace(R.id.fragment_container, fragment_message);
                        }
                        break;
                    case R.id.rd_menu_order:
                        if (SPUtils.get(MainActivity_R.this, "isLogin", "0").equals("0")) {
                            UiUtil.startActivity(MainActivity_R.this, LoginActivity.class);
                        } else if (SPUtils.get(MainActivity_R.this, "isLogin", "0").equals("1")) {
//已登录
                            transaction.replace(R.id.fragment_container, fragment_order);
                        }

                        break;
                    case R.id.rd_menu_person:
                        transaction.replace(R.id.fragment_container, fragment_person);
                        break;
                }
                transaction.commit();
            }
        });


//        super.initView();
    }

    @Override
    public void permissionSuccess(int requestCode) {
        super.permissionSuccess(requestCode);
        switch (requestCode) {
            case 0x004:
                Log.e("hhhhhhhhhhhhhhhhh", "16");
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + "4000000000"));
                startActivity(intent);
                break;
        }

    }

    //   * 显示toast，自己定义显示长短。
//            * param1:activity  传入context
//     * param2:word   我们需要显示的toast的内容
//     * param3:time length  long类型，我们传入的时间长度（如500）
//            */
    public static void showToast(final Activity activity, final String word, final long time) {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                final Toast toast = Toast.makeText(activity, word, Toast.LENGTH_LONG);
                toast.show();
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        toast.cancel();
                    }
                }, time);
            }
        });
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - mExitTime > 2000) {
                    showToast(this, "再按一次退出程序", 300);
                    //如果两次按键时间间隔大于2秒，则不退出
//                    Toast.makeText(this, "再按一次退出程序！",Toast.LENGTH_SHORT).show();
                    mExitTime = secondTime;//更新firstTime
                    return true;
                } else {
                    MyApp.getInstance().exit();//两次按键小于2秒时，退出应用
                    System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @OnClick(R.id.img_search)
    public void onViewClicked() {
        UiUtil.startActivity(this, SearchActivity.class);
    }
}
